package pt.ipleiria.plp;

import pt.ipleiria.plp.Lixeiras.LixeiraPeddingComplete;
import pt.ipleiria.plp.Lixeiras.LixeiraSent;
import de.enough.polish.ui.Alert;
import de.enough.polish.ui.AlertType;
import de.enough.polish.ui.Display;
import de.enough.polish.util.Locale;
import java.io.IOException;
import javax.microedition.midlet.MIDlet;
import pt.ipleiria.plp.threads.GetLixeiraSentFromServer;
import pt.ipleiria.plp.ui.AppKeyScreen;
import pt.ipleiria.plp.ui.EditarEventoScreen;
import pt.ipleiria.plp.ui.MainMenuScreen;

/**
 * @author Ricardo
 */
public class BlackSpotterMidlet extends MIDlet implements Runnable {

    private Display display;
    private MainMenuScreen mainMenu;
    private AppKeyScreen appKeyScreen;
    private Application app;

    public void startApp() {

        display = Display.getDisplay(this);
        app = new Application();
        mainMenu = new MainMenuScreen(MainMenuScreen.IMPLICIT, this);
        appKeyScreen = new AppKeyScreen(this);

        if (app.getAppKey() == null) {
            getDisplay().setCurrent(appKeyScreen);
        } else {

            getDisplay().setCurrent(mainMenu);
        }
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
        try {
            app.save();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        notifyDestroyed();
    }

    public void run() {
    }

    public Display getDisplay() {
        return display;
    }

    public MainMenuScreen getMainMenu() {
        return mainMenu;
    }

    /**
     * @return the app
     */
    public Application getApp() {
        return app;
    }

    public void goToMenu() {
        this.getDisplay().setCurrent(mainMenu);
    }

    public void editLixeira(String guid, boolean enviada) {

        if (enviada) {

            LixeiraSent lix = getApp().getManager().searchLixeirasEnviadas(guid);

            if (lix.getBs() == null) {

                GetLixeiraSentFromServer get = new GetLixeiraSentFromServer(lix, this);
                get.start();

                /*



                METER ECRA AQUI




                 */
                for (int i = 0; i < 10; i++) {
                    System.out.println("NAO ESQUECER LINHA 81 Blacspotter!!");
                }

            } else {

                EditarEventoScreen edit = new EditarEventoScreen(this, lix);
                this.getDisplay().setCurrent(edit);

            }



        } else {
            LixeiraPeddingComplete lix = getApp().getManager().searchLixeirasPorEnviadar(guid);

            if (lix == null) {
                //#style popupAlert
                Alert alert = new Alert(Locale.get("title.lixnotfound"),
                        Locale.get("mensagens.lixnotfound") + lix.getGuid(), null, AlertType.ERROR);
                this.getDisplay().setCurrent(alert, mainMenu);
            }

            EditarEventoScreen edit = new EditarEventoScreen(this, lix);
            this.getDisplay().setCurrent(edit);

        }



    }

    /**
     * A callback function the recives the confirmation from the thread
     * that gets the data from the server
     * 
     * @param lix The lixeira the which data was grabbed
     * @param error the error message (null if non)
     */
    public void callbackFromGetLixeiraSent(LixeiraSent lix, String error) {

        if (error != null) {

            //#style popupAlert
            Alert alert = new Alert(Locale.get("title.serverContact"),
                    Locale.get("mensagem.serverContact"), null, AlertType.ERROR);
            this.getDisplay().setCurrent(alert, mainMenu);

        } else {

            EditarEventoScreen edit = new EditarEventoScreen(this, lix);
            this.getDisplay().setCurrent(edit);

        }



    }
}


